package demo.hdz.mediacodecdecode.customview;

import android.view.View;
import android.widget.FrameLayout;

import demo.hdz.mediacodecdecode.Logger;

/**
 * @author hdz
 * @date 2022/06/23 09:43
 */
public class VideoViewUtil {
    private static final String TAG = "VideoViewUtil";

    public void adjustSize(View view, int screenWidth, int screenHeight, int videoWidth, int videoHeight) {
        Logger.d(TAG + ",adjustSize: " + screenWidth + ", " + screenHeight + ", " + videoWidth + ", " + videoHeight);
        int width = screenWidth;
        int height = screenHeight;

        view.setX(0);
        view.setY(0);

        //如果当前屏幕是宽屏
        if (screenWidth > screenHeight) {
            //如果当前视频为竖屏
            if (videoHeight > videoWidth) {
                width = videoWidth * height / videoHeight;
                float x = (float) (screenWidth - width) / 2.0f;
                view.setX(x);
            } else {
                width = videoWidth * height / videoHeight;
                float x = (float) (screenWidth - width) / 2.0f;
                view.setX(x);
            }
        } else {
            //如果当前视频为宽屏
            if (videoHeight < videoWidth) {
                height = videoHeight * width / videoWidth;
                float y = (float) (screenHeight - height) / 2.0f;
                view.setY(y);
            }
        }

        Logger.d(TAG + ",adjustSize: width: " + width + ", height: " + height);

        //设置SurfaceView大小为全屏幕，原来设置的尺寸是视频实际的尺寸_w*_h
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
        view.setLayoutParams(params);
    }
}
